mod commands;

use clap::{Parser, Subcommand};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[derive(Parser)]
#[command(name = "hysteria")]
#[command(about = "Hysteria 2 - A powerful proxy tool", long_about = None)]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Run Hysteria server
    Server {
        /// Config file path
        #[arg(short, long, default_value = "config/server.yaml")]
        config: String,
    },
    /// Run admin panel
    Admin {
        /// Config file path
        #[arg(short, long, default_value = "config/admin.yaml")]
        config: String,
    },
    /// Show version information
    Version,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Initialize tracing
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "hysteria=info,tower_http=debug".into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    let cli = Cli::parse();

    match cli.command {
        Commands::Server { config } => {
            commands::server::run(&config).await?;
        }
        Commands::Admin { config } => {
            commands::admin::run(&config).await?;
        }
        Commands::Version => {
            commands::version::show();
        }
    }

    Ok(())
}
